<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link href="../static/resources/bootstrap/css/bootstrap.css" rel="stylesheet">
    <link href="../static/resources/bootstrap/css/bootstrapValidator.min.css" rel="stylesheet">
    <link href="../static/resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="../static/resources/css/style.css" rel="stylesheet">
    <link href="../static/resources/css/exam.css" rel="stylesheet">
    <!-- Javascript files -->
    <!-- jQuery -->
    <script type="text/javascript" src="../static/resources/js/jquery/jquery-3.1.1.min.js"></script>
    <!-- Bootstrap JS -->
    <script type="text/javascript" src="../static/resources/bootstrap/js/bootstrap.js"></script>
    <script type="text/javascript" src="../static/resources/bootstrap/js/bootstrapValidator.min.js"></script>
    <script type="text/javascript" src="../static/resources/js/all.js"></script>
    <script type="text/javascript" src="../static/resources/js/register.js"></script>
    <style type="text/css">
        .form-group {
            margin-bottom: 5px;
            height: 59px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="lrform">
                <form role="form" id="form-create-account">
                    <input type="hidden" id="roleId" value="${roleId}">
                    <div class="form-group">
                        <span class="form-label"><span class="warning-label">*</span>用户名：</span>
                        <input type="text" class="form-control" id="username" maxlength="20" name="username"
                               style="width: 200px;">
                    </div>
                    <div class="form-group">
                        <span class="form-label"><span class="warning-label">*</span>密码：</span>
                        <input type="password" class="form-control" id="password" maxlength="20" name="password"
                               style="width: 200px;">
                    </div>
                    <div class="form-group">
                        <span class="form-label"><span class="warning-label">*</span>真实姓名：</span>
                        <input type="text" class="form-control" id="truename" maxlength="20" name="trueName"
                               style="width: 200px;">
                    </div>
                    <div class="form-group">
                        <span class="form-label"><span class="warning-label">*</span>身份证号：</span>
                        <input type="text" class="form-control" id="cardId" maxlength="18" style="width: 200px;"
                               name="cardId">
                    </div>
                    <div class="form-group">
                        <span class="form-label">手机：</span>
                        <input type="text" class="form-control" id="phone" maxlength="18" style="width: 200px;"
                               name="phone">
                    </div>
                    <div class="form-group">
                        <span class="form-label"><span class="warning-label">*</span>邮箱：</span>
                        <input type="text" class="form-control" id="email" maxlength="60" style="width: 200px;"
                               name="email">
                    </div>
                    <div class="form-group">
                        <span class="form-label"><span class="warning-label"></span>班级：</span>
                        <select id="department" class="form-control" name="depart">
                        </select>
                    </div>
                    <div class="form-group">
                        <div class="col-md-9 col-md-offset-3">
                            <button type="button" class="btn btn-info" id="btn-add">确定添加</button>
                            <button type="reset" class="btn btn-info">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

</div>
</body>
</html>
<script>

    var getAllDep = function () {
        $.ajax({
            type: "POST",
            url: "/getAllDep",
            dataType: "JSON",
            success: function (data) {
                //console.log(data);
                $(".deps").remove();
                $.each(data, function (idx, obj) {
                    var v = "<option class='deps' value='" + obj.depId + "'>" + obj.depName + "</option>";
                    $("#department").append(v);
                });
            }
        });
    };

    getAllDep();

    $("#btn-add").click(function () {
        $("#form-create-account").bootstrapValidator('validate');//提交验证
        if ($("#form-create-account").data('bootstrapValidator').isValid()) {//获取验证结果，如果成功，执行下面代码
            addUser();
        }
    });

    var addUser = function () {
        $.ajax({
            type: "POST",
            url: "/addUser",
            data: {
                userName: $("#username").val(),
                trueName: $("#truename").val(),
                nationalId: $("#cardId").val(),
                password: $("#password").val(),
                email: $("#email").val(),
                phoneNum: $("#phone").val(),
                enabled: 1,
                fieldId: 0,
                depId: $("#department").val(),
                roleId: $("#roleId").val()
            },
            success: function (data) {
                if (data == "success") {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.getUserByPage();
                    parent.layer.close(index);
                }
            }

        });
    };

    $('#form-create-account').bootstrapValidator({
        live: 'enabled',//验证时机，enabled是内容有变化就验证（默认），disabled和submitted是提交再验证
        excluded: [':disabled', ':hidden', ':not(:visible)'],//排除无需验证的控件，比如被禁用的或者被隐藏的
        submitButtons: '#btn-add',//指定提交按钮，如果验证失败则变成disabled
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须输入用户名'
                    },
                    stringLength: {//检测长度
                        min: 2,
                        max: 20,
                        message: '长度必须在2-20之间'
                    },
                    regexp: {//正则验证
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '所输入的字符不符要求'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须输入密码'
                    },
                    stringLength: {//检测长度
                        min: 6,
                        max: 18,
                        message: '长度必须在6-18之间'
                    },
                    regexp: {//正则验证
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '所输入的字符不符要求'
                    }
                }
            },
            trueName: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须输入真实姓名'
                    },
                    stringLength: {//检测长度
                        min: 2,
                        max: 20,
                        message: '长度必须在2-20之间'
                    }
                }
            },
            cardId: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须输入证件号'
                    },
                    regexp: {//正则验证
                        regexp: /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/,
                        message: '身份证格式不对'
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须输入手机号'
                    },
                    regexp: {//正则验证
                        regexp: /^[1-9]\d*$/,
                        message: '必须为正整数'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须输入邮箱'
                    },
                    regexp: {//正则验证
                        regexp: /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/,
                        message: '邮箱格式不对'
                    }
                }
            },
            depart: {
                validators: {
                    notEmpty: {//检测非空,radio也可用
                        message: '必须选择班级'
                    }
                }
            }
        }
    });
</script>